//
//  PAHomeScrollerView.m
//  PAMobileWallet
//
//  Created by shen pu on 13-7-15.
//  Copyright (c) 2013年 平安付. All rights reserved.
//

#import "PABankCardView.h"
#import "PABankCardInfo.h"
#import "PABaseView.h"
#import "UIColor+Extend.h"

@implementation PABankCardView
@synthesize bankCardBgImage;
@synthesize bankCardImage;
@synthesize bankCardNameImage;
@synthesize bankCardNumLable;
@synthesize bankCardTypeable;
@synthesize bankCardNameLable;
@synthesize lineImageView;
@synthesize bankCardAddButton;

@synthesize PA_delegate;


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        CGFloat x, y, w, h;
        x = 0;  y = 0;  w = 280;  h = 230/2 + 14/2;
        self.frame = CGRectMake(x, y, w, h);
        
        /*
        x = (280-(456/2))/2 ;  y = 230/2;  w = 456/2;  h = 14/2;
        lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, w, h)];
        lineImageView.image = [UIImage imageNamed:@"account_cardsink.png"];
        [self addSubview:lineImageView];
        */
        
        //x = (280-215)/2;  y = 6;  w = 430/2;  h = 230/2;
        x = (280-(456/2))/2 ;  y = 14/2;  w = 456/2;  h = 230/2;
        bankCardBgImage = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, w, h)];
        bankCardBgImage.image = [UIImage imageNamed:@"account_card2.png"];
        [self addSubview:bankCardBgImage];
        
        x = (280-215)/2 + 8;  y = 20;  w = 100;  h = 70/2;
        bankCardImage = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, w, h)];
        [self addSubview:bankCardImage];

        /*
        x = (280-210)/2 + 8 + 20 + 5;  y = 8;  w = 200/2;  h = 40/2;
        bankCardNameImage = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, w, h)];
        bankCardNameImage.image = [UIImage imageNamed:@"account_button.png"];
        [self addSubview:bankCardNameImage];
        */
        
        x = (280-215)/2 + 8 + 100 + 20 + 10;  y = 20;  w = 80;  h = 70/2;
        bankCardTypeable = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)];
        bankCardTypeable.textAlignment = NSTextAlignmentLeft;
        bankCardTypeable.backgroundColor = [UIColor clearColor];
        bankCardTypeable.textColor = [UIColor colorWithHexString:@"#ffffff"];
        bankCardTypeable.font = [UIFont boldSystemFontOfSize:16.0];
        [self addSubview:bankCardTypeable];

        x = (280-215)/2 + 10 ;  y = (230/2)-60 - 5 + 20;  w = 400/2;  h = 60/2;
        bankCardNumLable = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)];
        bankCardNumLable.textAlignment = NSTextAlignmentCenter;
        bankCardNumLable.textColor = [UIColor whiteColor];
        bankCardNumLable.backgroundColor = [UIColor clearColor];
        bankCardNumLable.textColor = [UIColor colorWithHexString:@"#ffffff"];
        bankCardNumLable.font = [UIFont systemFontOfSize:18.0];
        bankCardNumLable.font = [UIFont boldSystemFontOfSize:16.0];
        [self addSubview:bankCardNumLable];
        
        
        x = (280-215)/2;  y = 0;  w = 430/2;  h = 230/2;
        bankCardAddButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [bankCardAddButton setBackgroundColor:[UIColor clearColor]];
        bankCardAddButton.frame = CGRectMake(x, y, w, h);
        [bankCardAddButton addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:bankCardAddButton];

    }
    return self;
}

- (void)pressButton:(UIButton *)sender
{
    if (PA_delegate)
    {
        [PA_delegate sendHomeScroller:self sendButton:sender];
    }
}

- (id)initWithCardInfo:(PABankCardInfo *)bankCardInfo
{
    self = [self initWithFrame:CGRectMake(0, 0, 280, 239/2 + 14/2)];
    if (self) {
        [self loadCardInfo:bankCardInfo];
    }
    return self;
}
- (void)loadCardInfo:(PABankCardInfo *)bankCardInfo
{
    if(!bankCardInfo){
        self.bankCardBgImage.image = [UIImage imageNamed:@"account _addcard.png"];
        self.bankCardAddButton.enabled = YES;
        self.bankCardImage.hidden = YES;
        self.bankCardNameImage.hidden = YES;
        self.bankCardNumLable.hidden = YES;
        self.bankCardNameLable.hidden = YES;
    }else{
        
        NSString * cardMarkStr = bankCardInfo.bankMark;
        NSArray * cardNameArray  = [BANKCARD_MARK componentsSeparatedByString:@","];
        
        NSString * bankCardTypeStr;
        if (bankCardInfo.bankCardType == BCT_DEBIT_CARD)
        {
            bankCardTypeStr = @"信用卡";
        }
        
        if (bankCardInfo.bankCardType == BCT_CREDIT_CARD)
        {
            bankCardTypeStr = @"借记卡";
        }
        
        for (int i = 0; i < [cardNameArray count]; i++)
        {
            NSString * cardNameString = [cardNameArray objectAtIndex:i];
            if ([cardMarkStr isEqualToString:cardNameString])
            {
                self.bankCardNumLable.text = [NSString stringWithFormat:@"%@ **** **** **** %@",[bankCardInfo.bankCardNum substringToIndex:4],[bankCardInfo.bankCardNum substringFromIndex:[bankCardInfo.bankCardNum length] - 4]];
                self.bankCardImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"b%@.png",cardNameString]];
                self.bankCardTypeable.text = bankCardTypeStr;
                break;
            }
        }
    }
}


@end
